﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class SelectNode : BasePage
    {
        private Lc_NodeBLL lnb = new Lc_NodeBLL();
        private Lc_ProcessBLL lpb = new Lc_ProcessBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!IsPostBack)
                {
                    BindProcess();

                    if (Session["NodeBufferMemory1"] != null)
                    {
                        ddlProcess.SelectedValue = Session["NodeBufferMemory1"].ToString();
                    }
                    if (Session["NodeBufferMemory2"] != null)
                    {
                        txtName.Text = Session["NodeBufferMemory2"].ToString();
                    }
                    if (Session["NodeBufferMemory3"] != null)
                    {
                        ddlState.SelectedValue = Session["NodeBufferMemory3"].ToString();
                    }
                    if (Session["NodeBufferMemory4"] != null)
                    {
                        ddlType.SelectedValue = Session["NodeBufferMemory4"].ToString();
                    }

                    //Bind();
                }
            }
        }

        private void BindProcess()
        {
            DataTable dtProcess = lpb.GetAllInfo("", null);

            ddlProcess.DataSource = dtProcess;
            ddlProcess.DataTextField = "C_PROCESS_NAME";
            ddlProcess.DataValueField = "N_PROCESS_ID";
            ddlProcess.DataBind();
            ddlProcess.Items.Insert(0, new ListItem("---请选择---", "-1"));
            ddlProcess.SelectedValue = "-1";
        }

        private void Bind()
        {
            BindData(true, "");
        }

        private void BindData(bool isFirst, string can)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = "";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            rowcount = lnb.GetRecordCount(can, Where);

            DataTable dtItem;

            dtItem = lnb.GetListByPage(can, Where, "N_NODE_ID  DESC", PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dtItem != null && dtItem.Rows.Count > 0)
            {
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dtItem;
                PageIng.GridViewSource = grvShow;
                grvShow.DataSource = null;
                grvShow.DataBind();
                PageIng.Visible = false;
            }
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (ddlProcess.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择流程");
                return;
            }

            string whereStr = "";

            if (string.IsNullOrEmpty(txtName.Text.Trim()) && ddlProcess.SelectedValue.Equals("-1") && ddlState.SelectedValue.Equals("-1") && ddlType.SelectedValue.Equals("-1"))
            {
                Bind();
            }
            else
            {
                //拼接条件
                if (string.IsNullOrEmpty(txtName.Text.Trim()))
                {
                    whereStr += @"AND lcn.C_NODE_NAME like '%" + txtName.Text.Trim() + "%'";
                }
                if (ddlProcess.SelectedValue.ToString() != "-1")
                {
                    whereStr += "AND lcn.N_PROCESS_ID='" + ddlProcess.SelectedValue.ToString() + "'";
                }
                if (ddlState.SelectedValue.ToString() != "-1")
                {
                    whereStr += " AND lcn.N_NODE_STATE= '" + ddlState.SelectedValue.ToString() + "'";
                }
                if (ddlType.SelectedValue.ToString() != "-1")
                {
                    whereStr += " AND lcn.N_NODE_TYPE= '" + ddlType.SelectedValue.ToString() + "'";
                }
                if (txtName.Text != "")
                {
                    whereStr += " AND lcn.C_NODE_NAME LIKE '%" + txtName.Text + "%'";
                }

                ViewState["Where"] = whereStr;

                BindData(true, "Contion");
            }
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            Session["NodeBufferMemory1"] = ddlProcess.SelectedValue.ToString();
            Session["NodeBufferMemory2"] = txtName.Text;
            Session["NodeBufferMemory3"] = ddlState.SelectedValue.ToString();
            Session["NodeBufferMemory4"] = ddlType.SelectedValue.ToString();

            Response.Redirect("InsertNode.aspx");
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            string pid = "";
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                }
            }
            if (count == 1)
            {
                Session["NodeBufferMemory1"] = ddlProcess.SelectedValue.ToString();
                Session["NodeBufferMemory2"] = txtName.Text;
                Session["NodeBufferMemory3"] = ddlState.SelectedValue.ToString();
                Session["NodeBufferMemory4"] = ddlType.SelectedValue.ToString();
                MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                return;
                Response.Redirect("EditNode.aspx?id=" + pid);
            }
            else
            {
                MessageBox.Show(Page, "请选择数据");
                return;
            }
        }

        protected void btnDelele_Click(object sender, EventArgs e)
        {
            int count = 0;
            string pids = "";
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    //pids += ((HiddenField)grvShow.Rows[i].FindControl("HiddID")).Value + ",";
                    pids += ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip + ",";
                }
            }
            if (count > 0)
            {
                string strids = pids.Substring(0, pids.Length - 1);

                int row = lnb.DELETE(strids, lognUser.USER_NAME);
                if (row > 0)
                {
                    MessageBox.Show(Page, "删除成功");
                    Bind();
                    return;
                }
                else
                {
                    MessageBox.Show(Page, "删除失败");
                    return;
                }
            }
            else
            {
                MessageBox.Show(this, "请选择数据");
                return;
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false, "Contion");
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            DataTable dtItem = null;
            int rowcount = 0;
            //string ProcessName=ddlProcess.SelectedValue.ToString();

            string whereStr = "";

            string Where = "";

            if (string.IsNullOrEmpty(txtName.Text.Trim()) && ddlProcess.SelectedValue.Equals("-1") && ddlState.SelectedValue.Equals("-1") && ddlType.SelectedValue.Equals("-1"))
            {
                try
                {
                    Where = ViewState["Where"].ToString();
                }
                catch
                {
                    Where = "";
                }

                rowcount = lnb.GetRecordCount("", Where);

                dtItem = lnb.GetListByPage("", Where, "N_NODE_ID  DESC", PageIng.CurrentPage, PageIng.RowsPerPage);
            }
            else
            {
                //拼接条件
                if (string.IsNullOrEmpty(txtName.Text.Trim()))
                {
                    whereStr += @"AND lcn.C_NODE_NAME like '%" + txtName.Text.Trim() + "%'";
                }
                if (ddlProcess.SelectedValue.ToString() != "-1")
                {
                    whereStr += "AND lcn.N_PROCESS_ID='" + ddlProcess.SelectedValue.ToString() + "'";
                }
                if (ddlState.SelectedValue.ToString() != "-1")
                {
                    whereStr += " AND lcn.N_NODE_STATE= '" + ddlState.SelectedValue.ToString() + "'";
                }
                if (ddlType.SelectedValue.ToString() != "-1")
                {
                    whereStr += " AND lcn.N_NODE_TYPE= '" + ddlType.SelectedValue.ToString() + "'";
                }
                ViewState["Where"] = whereStr;

                try
                {
                    Where = ViewState["Where"].ToString();
                }
                catch
                {
                    Where = "";
                }

                rowcount = lnb.GetRecordCount("Contion", Where);

                dtItem = lnb.GetListByPage("Contion", Where, "N_NODE_ID  DESC", PageIng.CurrentPage, PageIng.RowsPerPage);
            }

            DataView view = dtItem.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            grvShow.DataSource = view;

            grvShow.DataBind();
        }
    }
}